<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE topic
  PUBLIC "-//OASIS//DTD DITA Composite//EN" "ditabase.dtd">
<topic id="topic1">
  <title id="cj20941">REFRESH MATERIALIZED VIEW</title>
  <body>
    <p id="sql_command_desc">Replaces the contents of a materialized view.</p>
    <section id="section2">
      <title>Synopsis</title>
      <codeblock id="sql_command_synopsis">REFRESH MATERIALIZED VIEW [ CONCURRENTLY ] <varname>name</varname>
    [ WITH [ NO ] DATA ]</codeblock>
    </section>
    <section id="section3">
      <title>Description</title>
      <p><codeph>REFRESH MATERIALIZED VIEW</codeph> completely replaces the contents of a
        materialized view. The old contents are discarded. To run this command you must be the
        owner of the materialized view. With the default, <codeph>WITH DATA</codeph>, the
        materialized view query is run to provide the new data, and the materialized view is
        left in a scannable state. If <codeph>WITH NO DATA</codeph> is specified, no new data is
        generated and the materialized view is left in an unscannable state. A query returns an
        error if the query attempts to access the materialized view.</p>
    </section>
    <section id="section4">
      <title>Parameters</title>
      <parml>
        <plentry>
          <pt>CONCURRENTLY</pt>
          <pd>Refresh the materialized view without locking out concurrent selects on the
            materialized view. Without this option, a refresh that affects a lot of rows tends to
            use fewer resources and completes more quickly, but could block other connections which
            are trying to read from the materialized view. This option might be faster in cases
            where a small number of rows are affected.<p>This option is only allowed if there is at
              least one <codeph>UNIQUE</codeph> index on the materialized view which uses only
              column names and includes all rows; that is, it must not index on any expressions nor
              include a <codeph>WHERE</codeph> clause.</p><p>This option cannot be used when the
              materialized view is not already populated, and it cannot be used with the
                <codeph>WITH NO DATA</codeph> clause.</p><p>Even with this option, only one
                <codeph>REFRESH</codeph> at a time may run against any one materialized
            view.</p></pd>
        </plentry>
        <plentry>
          <pt><varname>name</varname></pt>
          <pd>The name (optionally schema-qualified) of the materialized view to refresh. </pd>
        </plentry>
        <plentry>
          <pt>WITH [ NO ] DATA</pt>
          <pd><codeph>WITH DATA</codeph> is the default and specifies that the materialized view
            query is run to provide new data, and the materialized view is left in a scannable
            state. If <codeph>WITH NO DATA</codeph> is specified, no new data is generated and the
            materialized view is left in an unscannable state. An error is returned if a query
            attempts to access an unscannable materialized view.</pd>
          <pd><codeph>WITH NO DATA</codeph> cannot be used with <codeph>CONCURRENLY</codeph>. </pd>
        </plentry>
      </parml>
    </section>
    <section id="section5">
      <title>Notes</title>
      <p>While the default index for future <codeph><xref href="CLUSTER.xml"/></codeph> operations
        is retained, <codeph>REFRESH MATERIALIZED VIEW</codeph> does not order the generated rows
        based on this property. If you want the data to be ordered upon generation, you must use an
          <codeph>ORDER BY</codeph> clause in the materialized view query. However, if a
        materialized view query contains an <codeph>ORDER BY</codeph> or <codeph>SORT</codeph>
        clause, the data is not guaranteed to be ordered or sorted if <codeph>SELECT</codeph> is
        performed on the materialized view.</p>
    </section>
    <section id="section6">
      <title>Examples</title>
      <p>This command replaces the contents of the materialized view <codeph>order_summary</codeph>
        using the query from the materialized view's definition, and leaves it in a scannable
        state.</p>
      <codeblock>REFRESH MATERIALIZED VIEW order_summary;</codeblock>
      <p>This command frees storage associated with the materialized view
          <codeph>annual_statistics_basis</codeph> and leaves it in an unscannable state.</p>
      <codeblock>REFRESH MATERIALIZED VIEW annual_statistics_basis WITH NO DATA;</codeblock>
    </section>
    <section id="section7">
      <title>Compatibility</title>
      <p><codeph>REFRESH MATERIALIZED VIEW</codeph> is a Greenplum Database extension of the SQL
        standard.</p>
    </section>
    <section id="section8"><title>See Also</title><codeph><xref href="ALTER_MATERIALIZED_VIEW.xml"
        /></codeph>, <codeph><xref href="CREATE_MATERIALIZED_VIEW.xml"/></codeph>, <codeph><xref
          href="DROP_MATERIALIZED_VIEW.xml"/></codeph></section>
  </body>
</topic>
